<?php

namespace addons\TfFutures\api\controllers;

use addons\TfFutures\common\models\OrderReason;
use addons\TfFutures\common\models\Symbol;
use addons\TfFutures\services\OrderService;
use Yii;
use common\helpers\ResultDataHelper;
use api\controllers\OnAuthController;
use addons\TfFutures\common\models\Order;

class OrderReasonController extends OnAuthController
{
    public $modelClass = OrderReason::class;

    /**
     * 订单列表
     * @return string
     */
    public function actionList()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422, '用户id不能为空');
        }
        $state = Yii::$app->request->get('state', 'all');
        switch ($state) {
            case 'hold':
                $where = ['state' => '挂单'];
                break;
            case 'end':
                $where = ['state' => '完成'];
                break;
            case 'del':
                $where = ['state' => '撤单'];
                break;
            default:
                $where = ['>', 'id', 0];
                break;
        }
        $model = $this->modelClass::find()
                                  ->where($where)
                                  ->andWhere(['status' => 1, 'member_id' => $uid])
                                  ->asArray()
                                  ->all();
        foreach ($model as $k => $v) {
            $symbol                     = Symbol::findOne($v['symbol']);
            $model[$k]['symbol_detail'] = $symbol;
            $model[$k]['created_at']    = date('Y-m-d H:i:s', $model[$k]['created_at']);
            $model[$k]['updated_at']    = date('Y-m-d H:i:s', $model[$k]['updated_at']);
        }

        return ResultDataHelper::api(200, 'ok', $model);
    }

    /**
     * 完成,挂单,撤单列表
     * 当日委托
     * @return string
     */
    public function actionOlist()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422, '用户id不能为空');
        }
        $state = Yii::$app->request->get('state','all');
        $where = ['>', 'id', 0];
        $data = strtotime(date('Y-m-d 00:00:00'));
        $model = $this->modelClass::find()
            ->where($where)
            ->andWhere(['status' => 1, 'member_id' => $uid])
            ->andWhere(['>=','created_at',$data])
            ->andWhere(['state'=>'挂单'])
            ->asArray()
            ->all();
        foreach ($model as $k => $v) {
            $symbol                     = Symbol::find()->where(['code'=>$v['symbol']])->one();
            $model[$k]['symbol_detail'] = $symbol;
            $model[$k]['created_at']    = date('Y-m-d H:i:s', $model[$k]['created_at']);
            $model[$k]['updated_at']    = date('Y-m-d H:i:s', $model[$k]['updated_at']);
        }

        return ResultDataHelper::api(200, 'ok', $model);
    }
    /**
     * 结算列表
     * @return string
     */
    public function actionCounts()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422, '用户id不能为空');
        }
        $time = Yii::$app->request->get('time');
        if(!empty($time)) {
            $where['open_time'] = $time;
        }
        $where['state'] = '完成';
        $model = $this->modelClass::find()
            ->where($where)
            ->andWhere(['status' => 1, 'member_id' => $uid])
            ->asArray()
            ->all();
        foreach ($model as $k => $v) {
            $symbol                     = Symbol::find()->where(['code'=>$v['symbol']])->one();
            $model[$k]['symbol_detail'] = $symbol;
            $model[$k]['created_at']    = date('Y-m-d H:i:s', $model[$k]['created_at']);
            $model[$k]['updated_at']    = date('Y-m-d H:i:s', $model[$k]['updated_at']);
        }

        return ResultDataHelper::api(200, 'ok', $model);
    }

    /**
     * 挂单转持仓
     */
    //    public function actionPendingToPosition(){
    //        if (!Yii::$app->request->isPost) {
    //            return ResultDataHelper::api(304,'access done!');
    //        }
    //        $orderid = Yii::$app->request->post('orderid');
    //        list($status,$m) = OrderService::PendingToPosition($orderid);
    //
    //        if (!$status && !$m) {
    //            return ResultDataHelper::api(422,$m);
    //        }
    //        return ResultDataHelper::api(200,'成功');
    //    }
    /**
     * 撤单
     * @return mixed
     */
    public function actionKillOrder()
    {
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(403, 'Access done!');
        }
        $order_id = Yii::$app->request->post('order_id');
        if (empty($order_id)) {
            return ResultDataHelper::api(422, 'order_id 参数缺少 订单不存在');
        }
        $member_id = Yii::$app->user->identity->member_id;
        list($state, $res) = OrderService::killOrder($member_id, $order_id);
        if ($state) {
            return ResultDataHelper::api(200, '成功');
        }else {
            return ResultDataHelper::api(422, '失败', $res);
        }
    }

    /**
         * 全部撤单
     *
     * @return mixed
     * @throws \Exception
     */
    public function actionKillAll()
    {
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(403, 'Access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        //获取用户全部可以撤单的信息
        $order = OrderReason::find()
                      ->where(['=', 'member_id', $uid])
                      ->andWhere(['=', 'state', '挂单'])
                      ->all();
        if (empty($order)) {
            return ResultDataHelper::api(422, '用户没有订单');
        }
        foreach ($order as $item) {
            list($status, $m) = OrderService::killOrder($uid, $item->id);
        }
        if (!$status) {
            return ResultDataHelper::api(422, '全部撤单失败', $m);
        }

        return ResultDataHelper::api(200, '全部撤单成功');
    }

    //查询用户持仓明细
    public function actionTotal(){

        if(!Yii::$app->request->isGet){
            return ResultDataHelper::api(403,'Access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        $where['member_id'] = $uid;
        $where['state'] = "持仓";
        $order=Order::find()
            ->where($where)
            ->orderBy('open_time desc')
            ->all();
        if(empty($order)){
            return ResultDataHelper::api(422,'今日暂无持仓订单');
        }
        foreach($order as $k=>$v){

            $order[$k]['created_at']    = date('Y-m-d H:i:s', $order[$k]['created_at']);
            $order[$k]['updated_at']    = date('Y-m-d H:i:s', $order[$k]['updated_at']);
        }
        return ResultDataHelper::api(200, 'ok', $order);
    }
    //修改状态
    public function actionStatus(){
        if(!Yii::$app->request->isPost){
            return ResultDataHelper::api(403,'Access done!');
        }

        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422,'用户id不能为空');
        }

        $id=Yii::$app->request->post('id');
        if(empty($id)){
            return ResultDataHelper::api(422,'订单id不能为空');
        }

        $order_id=explode( ',',$id);

        $order = OrderReason::find()
            ->where(['in', 'id', $order_id])
            ->all();
        foreach($order as $v){
            $v->status = -1;
            if(!$v->save()){
            	return ResultDataHelper::api(422, '删除失败');
            }
        }
        // if (empty($order)){
        //     return ResultDataHelper::api(422,'订单不存在');
        // }
        // $status=Yii::$app->request->post('status');


            // $order->status="-1";



        return ResultDataHelper::api(200, '状态修改成功');
    }


}